package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.Building;
import com.yugong.entity.dto.Room;
import com.yugong.entity.vo.request.RoomRequestVO;
import com.yugong.entity.vo.response.RoomListResponseVO;
import com.yugong.mapper.BuildingMapper;
import com.yugong.mapper.RoomMapper;
import com.yugong.service.RoomService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房间业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/11
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 根据房间名查询房间
     *
     * @param pageNum  当前页
     * @param pageSize 当前页的数据个数
     * @param roomName 房间名
     * @return 房间
     */
    @Override
    public PageInfo<RoomListResponseVO> findRoom(Integer pageNum, Integer pageSize, String roomName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLambdaQueryWrapper
                .like(Room::getRoomName, roomName)
                .orderByDesc(Room::getRoomCreateTime);
        List<Room> roomList = roomMapper.selectList(roomLambdaQueryWrapper);
        QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();
        roomQueryWrapper.like("room_name", roomName);
        Long roomCountNumber = roomMapper.selectCount(roomQueryWrapper);
        List<RoomListResponseVO> roomListResponseVOList = new ArrayList<>();
        for (Room room : roomList) {
            RoomListResponseVO roomListResponseVO = new RoomListResponseVO();
            roomListResponseVO.setRoomNo(room.getRoomNo());
            roomListResponseVO.setRoomName(room.getRoomName());
            roomListResponseVO.setRoomBuildingNo(room.getRoomBuildingNo());
            roomListResponseVO.setRoomBuildingNo(room.getRoomBuildingNo());
            Building building = buildingMapper.selectById(room.getRoomBuildingNo());
            if (building == null) {
                roomListResponseVO.setRoomBuildingName("未填写");
            } else {
                roomListResponseVO.setRoomBuildingName(building.getBuildingName());
            }
            roomListResponseVO.setRoomCreateMan(room.getRoomCreateMan());
            String createTime = timeUtils.setDateToStrDate(room.getRoomCreateTime(), Const.YYYY_MM_DD);
            roomListResponseVO.setRoomCreateTime(createTime);
            if (room.getRoomUpdateMan() == null) {
                roomListResponseVO.setRoomUpdateMan("未更新");
            } else {
                roomListResponseVO.setRoomUpdateMan(room.getRoomUpdateMan());
            }
            if (room.getRoomUpdateTime() == null) {
                roomListResponseVO.setRoomUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(room.getRoomUpdateTime(), Const.YYYY_MM_DD);
                roomListResponseVO.setRoomUpdateTime(updateTime);
            }
            roomListResponseVO.setRoomCountNumber(roomCountNumber);
            roomListResponseVOList.add(roomListResponseVO);
        }
        return PageInfo.of(roomListResponseVOList);
    }

    /**
     * 新增房间
     *
     * @param roomRequestVO 房间请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int addRoom(RoomRequestVO roomRequestVO) {
        Room room = new Room();
        room.setRoomNo(roomRequestVO.getRoomNo());
        room.setRoomName(roomRequestVO.getRoomName());
        room.setRoomBuildingNo(roomRequestVO.getRoomBuildingNo());
        room.setRoomCreateMan(roomRequestVO.getRoomCreateMan());
        room.setRoomCreateTime(new Date());
        return roomMapper.insert(room);
    }

    /**
     * 更新房间
     *
     * @param roomRequestVO 房间请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateRoom(RoomRequestVO roomRequestVO) {
        Room room = new Room();
        room.setRoomNo(roomRequestVO.getRoomNo());
        room.setRoomName(roomRequestVO.getRoomName());
        room.setRoomBuildingNo(roomRequestVO.getRoomBuildingNo());
        room.setRoomUpdateMan(roomRequestVO.getRoomUpdateMan());
        room.setRoomUpdateTime(new Date());
        return roomMapper.updateById(room);
    }

    /**
     * 删除房间
     *
     * @param roomNo 房号
     * @return 数据库影响行数
     */
    @Override
    public int deleteRoom(String roomNo) {
        return roomMapper.deleteById(roomNo);
    }

    /**
     * 查询房间
     *
     * @return 房间
     */
    @Override
    public List<RoomRequestVO> findRoom() {
        LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Room> roomList = roomMapper.selectList(roomLambdaQueryWrapper);
        List<RoomRequestVO> roomRequestVOList = new ArrayList<>();
        for (Room room : roomList) {
            RoomRequestVO roomRequestVO = new RoomRequestVO();
            roomRequestVO.setRoomNo(room.getRoomNo());
            roomRequestVO.setRoomName(room.getRoomName());
            roomRequestVOList.add(roomRequestVO);
        }
        return roomRequestVOList;
    }
}
